﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using TcpLabCommon;
using System.Data;
namespace Common.GeneralClass
{
    public class ChannelDB
    {
        public string UnitID;
        public string UnitName;
        public DateTime InsertTime;
        public DataPacket ChannelData;
        public string InsertPeople;
        public string ProjectName;
        public string ItemName;
    }

    public class OperateChannel 
    {
        MyDb mydb = Mdl_Class.Mydb;
        MyDb mydb_other = Mdl_Class.Mydb_other;
        //添加用户信息
        public int AddChannel(ChannelDB cdb)
        {
            int i = 0;
            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("insert into T_B_Channel(UnitID,UnitName,ChannelData,InsertDate,InsertPeople,ProjectName,ItemName) values('{0}','{1}',@abc,GetDate(),{2}','{3}','{4}')", cdb.UnitID, cdb.UnitName, cdb.InsertPeople, cdb.ProjectName, cdb.ItemName);
                    byte[] b = Activator.CreateInstance<StructHelper>().Serialize(cdb.ChannelData);
                    i = mydb.ExecuteNonQuery1(sql, b);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddChannel(ChannelDB cdb)", ex.ToString());
                }
            }
            
            return i;
        }


        public int AddChannel_oher(ChannelDB cdb)
        {
            int i = 0;
            using (mydb_other)
            {
                try
                {

                    mydb_other.Open();
                    string sql = string.Format("insert into T_B_Channel(UnitID,UnitName,ChannelData,InsertDate,InsertPeople,ProjectName,ItemName) values('{0}','{1}',@abc,GetDate(),{2}','{3}','{4}')", cdb.UnitID, cdb.UnitName, cdb.InsertPeople, cdb.ProjectName, cdb.ItemName);
                    byte[] b = Activator.CreateInstance<StructHelper>().Serialize(cdb.ChannelData);
                    i = mydb_other.ExecuteNonQuery1(sql, b);
                    mydb_other.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddChannel(ChannelDB cdb)", ex.ToString());
                }
            }
            return i;
        }

        public int AddTestData(DataPacket dtpack)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    //System.Diagnostics.Stopwatch sp = new Stopwatch();
                    mydb.Open();
                   
                     byte[] b = Activator.CreateInstance<StructHelper>().Serialize(dtpack);
                    //String buffer = Activator.CreateInstance<StringSerializeHelper>().DeSerialize<String>(b);
                    //string sql = string.Format("insert into T_B_Test(UnitID,ChannelData,InsertTime) values('{0}','{1}','{2}')", "1", b, DateTime.Now);
                    //i = mydb.ExecuteNonQuery1(b);
                    //i = mydb.ExecuteNonQuery2(b);
                    //System.Threading.Thread.Sleep(2000);
                    mydb.Close();
                }   
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddTestData(DataPacket dtpack)", ex.ToString());
                }
            }
            return i;
        }


        //获取所有
        public List<byte[]> GetTestData_test()
        {
            List<byte[]> testlist = new List<byte[]>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_B_Test where InsertTime between '2014/11/4 17:00:00' and '2014/11/4 19:00:00'";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        //string o = (byte[])mydb.DataReader["ChannelData"];
           
                        //byte[] by =   Activator.CreateInstance<StringSerializeHelper>().Serialize<string>(o);
                     //   byte[] by= HexStrToByteArray(mydb.DataReader["ChannelData"].ToString());
                        byte[] by1 = mydb.DataReader["ChannelData"] as byte[];
                        DataPacket dpk = Activator.CreateInstance<StringSerializeHelper>().DeSerialize<DataPacket>(by1);
                        testlist.Add(by1);
                     //string a =   by1.ToString();
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllUsers()", ex.ToString());
                }
            }
            return testlist;
        }


        //获取所有
        public List<byte[]> GetTestData_test2()
        {
            List<byte[]> testlist = new List<byte[]>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_B_Test2 where InsertTime between '2014/11/4 17:00:00' and '2014/11/4 19:00:00'";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        //string o = (byte[])mydb.DataReader["ChannelData"];

                        //byte[] by =   Activator.CreateInstance<StringSerializeHelper>().Serialize<string>(o);
                        //   byte[] by= HexStrToByteArray(mydb.DataReader["ChannelData"].ToString());
                        byte[] by1 = mydb.DataReader["ChannelData"] as byte[];
                        testlist.Add(by1);
                        //string a = by1.ToString();
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllUsers()", ex.ToString());
                }
            }
            return testlist;
        }


        //通过sql语句获取所有通道信息
        public DataTable GetChannelBySql(string sql,int channid)
        {
            DataTable dt = null;

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    //string sql = "select * from T_B_Test2 where InsertTime between '2014/11/4 17:00:00' and '2014/11/4 19:00:00'";
                    mydb.ExecuteReader(sql);

                    dt = new DataTable();
                    dt.Columns.Add("PK2PK");
                    dt.Columns.Add("GapVoltage");
                    dt.Columns.Add("RoomSquareValue");
                    dt.Columns.Add("OneMultiplierAmp");
                    dt.Columns.Add("OneMultiplierPhase");
                    dt.Columns.Add("TWOMultiplierAmp");

                    while (mydb.DataReader.Read())
                    {
                        //string o = (byte[])mydb.DataReader["ChannelData"];

                        //byte[] by =   Activator.CreateInstance<StringSerializeHelper>().Serialize<string>(o);
                        //   byte[] by= HexStrToByteArray(mydb.DataReader["ChannelData"].ToString());
                        byte[] by1 = mydb.DataReader["ChannelData"] as byte[];
                        DataPacket dpk = Activator.CreateInstance<StringSerializeHelper>().DeSerialize<DataPacket>(by1);
                        //testlist.Add(by1);
                        //string a = by1.ToString();

                        DataRow dr = dt.NewRow();
                        dr["PK2PK"] = dpk.PK2PK[channid];
                        dr["GapVoltage"] = dpk.GapVoltage[channid];
                        dr["RoomSquareValue"] = dpk.RoomSquareValue[channid];
                        dr["OneMultiplierAmp"] = dpk.OneMultiplierAmp[channid];
                        dr["OneMultiplierPhase"] = dpk.OneMultiplierPhase[channid];
                        dr["TWOMultiplierAmp"] = dpk.TWOMultiplierAmp[channid];
                        dt.Rows.Add(dr);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetChannelBySql()", ex.ToString());
                }
            }
            return dt;
        }

        private  byte[] HexStrToByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                throw new ArgumentException("十六进制字符串长度不对");
            byte[] buffer = new byte[hexString.Length / 2];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 0x10);
            }
            return buffer;
        }

    }
}
